<!--老师查看活动报名情况列表-->
<style>
	.kechengxiangqing {
		margin-bottom: 0;
		border-bottom: 0;
	}
	
	.kechengxiangqing tr>th {
		border-bottom: 0;
	}
	
	#daochu {
		margin-bottom: 20px;
		float: right;
	}
	
	.piliang {
		margin-right: 5px;
		float: left;
	}
	
	.count {
		margin-bottom: 20px;
		margin-right: 20px;
	}
</style>

<button class="btn btn-primary count" type="button"> 待审核 <span class="badge" id="count_shenhe">0</span></button>
<button class="btn btn-primary count" type="button"> 已通过 <span class="badge" id="count_pass">0</span></button>
<button class="btn btn-primary count" type="button"> 未通过 <span class="badge" id="count_nopass">0</span></button>

<a class="btn btn-primary huoqu" id="daochu" href="#">导出报名情况表</a>

<table class="table table-striped table-bordered table-hover kechengxiangqing">
	<tr>
		<th id="a_title" class="text-center">活动报名情况</th>
	</tr>
</table>

<table class="table table-striped table-bordered table-hover text-center table-test" id="table1">
	<!--<thead>
		<tr class="top">
			<td class="" style="width: 15%;"><b>学号</b></td>
			<td class="" style="width: 15%;"><b>姓名</b></td>
			<td class="" style="width: 15%;"><b>电话</b></td>
			<td class="" style="width: 20%;"><b>邮箱</b></td>
			<td colspan="2" class="" style="width: 10%;"><b>审核操作</b></td>
		</tr>
	</thead>-->
</table>

<nav aria-label="...">
	<a class="btn btn-primary piliang" id="quanxuan" href="#">全选</a>
	<a class="btn btn-primary piliang" id="pl_pass" href="#">批量通过</a>
	<a class="btn btn-primary piliang" id="pl_nopass" href="#">批量拒绝</a>
	<ul class="pager">
		<li>
			<a href="#" id="pre">上一页</a>
		</li>
		<li>
			<a href="#" id="top">首页</a>
		</li>
		<li>第<span class="num">1</span>页</li>
		<li>
			<a href="#" id="end">尾页</a>
		</li>
		<li>
			<a href="#" id="next">下一页</a>
		</li>
	</ul>
</nav>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button class="close" data-dismiss="modal"> &times;</button>
				<h4 class="text-danger">提示</h4>
			</div>
			<div class="modal-body">
				<h5>操作成功！</h5>
			</div>
			<div class="modal-footer">
				<a class="btn btn-primary" data-dismiss="modal" id="pass_modal">确认</a>
			</div>
		</div>
	</div>
</div>

<script>
	scroll(0, 0);

	$('#daochu').click(function() {
		var value = 'teacher/a-downLoad?aid=' + $.session.get("aid")
		window.open(getFullURL(value));
	})

	$('#pass_modal').click(function() {
		setTimeout(function() {
			$('.center').html('');
			$('.center').load('./teacherActivity.html');
		}, 300)
	})

	//全局变量
	var pageSize = 15;
	var myajax;

	$.ajax({ //初始化标题
		xhrFields: {
			withCredentials: true
		},
		type: "GET",
		dataType: 'json',
		url: getFullURL('get-one-a'),
		data: {
			aid: $.session.get("aid")
		},
		success: function(data) {
			console.log(data)
			if(data.status == 0) {
				$('#a_title').html("\"" + data.activity.title + "\"" + "活动报名情况");
			}
		},
		error: function(err) {
			alert("请求异常，请重新登录！", function() {
				$(location).attr('href', './login.html');
			})
		}
	})

	function pl_shenhe(pl_sno, flag) {
		$.ajax({ //审核活动
			xhrFields: {
				withCredentials: true
			},
			type: "post",
			dataType: 'json',
			url: getFullURL('teacher/audit'),
			data: {
				aid: $.session.get("aid"),
				type: flag,
				array: pl_sno
			},
			success: function(data) {
				console.log(data)
				if(data.status == 0) {
					$(myModal).modal({
						keyboard: true,
						show: true
					})
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	}

	function fenye(pageNum) {
		myajax = $.ajax({ //初始化标题
			xhrFields: {
				withCredentials: true
			},
			type: "post",
			dataType: 'json',
			url: getFullURL('teacher/activity-students'),
			data: {
				pageNum: pageNum,
				pageSize: pageSize,
				aid: $.session.get("aid")
			},
			success: function(data) {
				console.log(data)
				$('#count_shenhe').html(0);
				$('#count_pass').html(0);
				$('#count_nopass').html(0);

				if(data.status == 0) {
					$.session.set("prePageNum", data.page.prePageNum);
					$.session.set("nextPageNum", data.page.nextPageNum);
					$.session.set("totalPageNum", data.page.totalPageNum);
					$('.num').html(data.page.pageNum);
					$('#table1').html("");
					var title = `
						<thead>
							<tr class="top">
								<td class="" style="width: 4%;"><input type="checkbox" class="quanxuan_box" id="quanxuan_box"/></td>
								<td class="" style="width: 15%;"><b>学号</b></td>
								<td class="" style="width: 15%;"><b>姓名</b></td>
								<td class="" style="width: 15%;"><b>电话</b></td>
								<td class="" style="width: 20%;"><b>邮箱</b></td>
								<td colspan="2" class="" style="width: 10%;"><b>审核操作</b></td>
							</tr>
						</thead>
					`;
					$('#table1').append(title);

					var str_ = `<tbody id="tbody_"></tbody>`;
					$('#table1').append(str_);
					$.each(data.page.list, function(i, n) {
						var str = `
								<tr id="tr_` + n.sno + `">
									<td><input type="checkbox" class="checkbox_xuanzhong" name="checkbox_xuanzhong" id="checkbox_xuanzhong" value="` + n.sno + `"/></td>
									<td>` + n.sno + `</td>
									<td>` + n.name + `</td>
									<td>` + n.phone + `</td>
									<td>` + n.email + `</td>
								</tr>
						`;
						$('#tbody_').append(str);

						var tr_id = "#tr_" + n.sno;
						if(n.status == 0) {
							var td = `<td><button type="button" class="btn btn-primary btn-xs agree" value="` + n.sno + `">同意</button></td>
									<td><button type="button" class="btn btn-primary btn-xs refuse" value="` + n.sno + `">拒绝</button></td>
									`;
							$(tr_id).append(td);
						} else if(n.status == 1) {
							var td = `<td colspan="2">审核通过</td>`;
							$(tr_id).append(td);
							$(tr_id).children("td:eq(0)").find("input").attr("type", "hidden");
						} else if(n.status == -1) {
							var td = `<td colspan="2">审核未通过</td>`;
							$(tr_id).append(td);
							$(tr_id).children("td:eq(0)").find("input").attr("type", "hidden");
						}
					})

					$('#count_shenhe').html(data.allRegist - data.registOk - data.registNotOk);
					$('#count_pass').html(data.registOk);
					$('#count_nopass').html(data.registNotOk);
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	}

	fenye(1);

	$("#top").click(function() {
		fenye(1);
	})
	$("#end").click(function() {
		fenye($.session.get("totalPageNum"));
	})
	$("#pre").click(function() {
		fenye($.session.get("prePageNum"));
	})
	$("#next").click(function() {
		fenye($.session.get("nextPageNum"));
	})

	$.when(myajax).done(function() {
		$(".agree").click(function() { //审核通过
			var list = $(this).val()
			pl_shenhe(list, 0);
		})

		$(".refuse").click(function() { //审核拒绝
			var list = $(this).val()
			pl_shenhe(list, -1);
		})

		$("#pl_pass").click(function() { //批量审核通过
			var list = '';
			$('#checkbox_xuanzhong:checked').each(function(i) {
				list = list + $(this).val() + ","
			});

			if($('#checkbox_xuanzhong:checked').length <= 0) {
				return;
			} else {
				pl_shenhe(list, 0);
			}
		})

		$("#pl_nopass").click(function() { //批量审核拒绝
			var list = '';
			$('#checkbox_xuanzhong:checked').each(function(i) {
				list = list + $(this).val() + ","
			});

			if($('#checkbox_xuanzhong:checked').length <= 0) {
				return;
			} else {
				pl_shenhe(list, -1);
			}
		})

		$('#quanxuan_box').change(function() { //全选
			if($('#quanxuan_box').is(':checked')) {
				$('.checkbox_xuanzhong').attr('checked', true); //兼容
				$('.checkbox_xuanzhong').prop('checked', true);
			} else {
				$('.checkbox_xuanzhong').attr('checked', false); //兼容
				$('.checkbox_xuanzhong').prop('checked', false);
			}
		})

		$('#quanxuan').click(function() {
			$('#quanxuan_box').attr('checked', true); //兼容
			$('#quanxuan_box').prop('checked', true);

			if($('#quanxuan_box').is(':checked')) {
				$('.checkbox_xuanzhong').attr('checked', true); //兼容
				$('.checkbox_xuanzhong').prop('checked', true);
			}
		})

	})
</script>